package com.cn.kehong.service;

import com.cn.kehong.api.IFactoryShiftService;
import com.cn.kehong.dao.FactoryShiftMapper;
import com.cn.kehong.dao.FactoryShiftOperateMapper;
import com.cn.kehong.domain.FactoryShift;
import com.cn.kehong.domain.FactoryShiftExample;
import com.cn.kehong.domain.FactoryShiftOperate;
import com.cn.kehong.domain.FactoryShiftOperateExample;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FactoryShiftServiceImpl implements IFactoryShiftService {

    @Autowired
    private FactoryShiftMapper factoryShiftMapper;
    @Autowired
    private FactoryShiftOperateMapper factoryShiftOperateMapper;


    @Override
    public List<FactoryShift> getFactoryShifts(QueryForm queryForm) throws Exception {
        List<FactoryShift> factoryShifts = factoryShiftMapper.selectFactoryShifts(queryForm);
        return factoryShifts;
    }

    @Override
    public FactoryShift getFactoryShiftByName(String factoryName,String shiftType) throws Exception {
        FactoryShiftExample example = new FactoryShiftExample();
        String[] split = factoryName.split("-");
        example.createCriteria().andFactoryNameEqualTo(split[0]).andShiftTypeEqualTo(shiftType).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<FactoryShift> factoryShifts = factoryShiftMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(factoryShifts)||factoryShifts.size() !=1){
            return null;
        }
        for (FactoryShift factoryShift:factoryShifts){
            FactoryShiftOperateExample operateExample = new FactoryShiftOperateExample();
            operateExample.createCriteria().andShiftIdEqualTo(factoryShift.getId());
            List<FactoryShiftOperate> factoryShiftOperates = factoryShiftOperateMapper.selectByExample(operateExample);
            if (factoryShiftOperates.size()>0){
                factoryShift.setFactoryShiftOperates(factoryShiftOperates);
            }
        }
        return factoryShifts.get(0);
    }



    @Override
    public String addFactoryShift(List<FactoryShift> factoryShifts) throws Exception {
        if (CollectionUtils.isEmpty(factoryShifts)) {
            return "数据为空！";
        }

        String factoryName = factoryShifts.get(0).getFactoryName();
        if (StringUtils.isEmpty(factoryName)) {
            return "厂房不能为空！";
        }

        // 创建查询条件
        FactoryShiftExample example = new FactoryShiftExample();
        example.createCriteria().andFactoryNameEqualTo(factoryName).andStatusEqualTo(Constants.NORMAL_STATUS);

        // 检查是否存在数据
        int count = factoryShiftMapper.countByExample(example);

        if (count > 0) {
            // 存在数据，先删除现有数据
            List<FactoryShift> existingShifts = factoryShiftMapper.selectByExample(example);

            for (FactoryShift existingShift : existingShifts) {
                // 删除子表相关的数据
                FactoryShiftOperateExample operateExample = new FactoryShiftOperateExample();
                operateExample.createCriteria().andShiftIdEqualTo(existingShift.getId());
                int deleteOperateCount = factoryShiftOperateMapper.deleteByExample(operateExample);
                if (deleteOperateCount < 0) {
                    return "删除子表数据异常！";
                }

                // 删除班次数据
                int deleteShiftCount = factoryShiftMapper.deleteByPrimaryKey(existingShift.getId());
                if (deleteShiftCount < 0) {
                    return "删除主表数据异常！";
                }
            }
        }

        // 处理新增数据
        for (FactoryShift factoryShift : factoryShifts) {
            String id = CommonUtil.generateRandomNum("shift-");
            factoryShift.setId(id);
            factoryShift.setStatus("0");
            for (FactoryShiftOperate operate : factoryShift.getFactoryShiftOperates()) {
                operate.setShiftId(id);
            }
        }

        count = factoryShiftMapper.batchInsertFactoryShifts(factoryShifts);
        if (count < 1) {
            return "插入数据异常！";
        }

        for (FactoryShift shift : factoryShifts) {
            if (shift.getFactoryShiftOperates().size()>0){
                count = factoryShiftOperateMapper.batchInsertFactoryShiftsOperate(shift.getFactoryShiftOperates());
                if (count < 1) {
                    return "插入子表数据异常！";
                }
            }

        }

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateFactoryShift(FactoryShift factoryShift) throws Exception {
        // 检查传入的对象是否有效
        if (factoryShift == null || StringUtils.isEmpty(factoryShift.getId())) {
            return "无效的更新请求！";
        }

        // 更新操作
        int count = factoryShiftMapper.updateByPrimaryKeySelective(factoryShift);
        if (count > 0) {
            return "更新成功！";
        } else {
            return "未找到指定ID的记录，无法更新！";
        }
    }

    @Override
    public String deleteFactoryShiftById(String id) throws Exception {
        // 检查传入的ID是否有效
        if (StringUtils.isEmpty(id)) {
            return "无效的删除请求！";
        }

        int count = factoryShiftMapper.deleteByPrimaryKey(id);
        if (count > 0) {
            return "删除成功！";
        } else {
            return "未找到指定ID的记录，无法删除！";
        }
    }

    @Override
    public List<FactoryShift> getFactoryShiftsAll(QueryForm queryForm) throws Exception {
        FactoryShiftExample shift = new FactoryShiftExample();
        shift.createCriteria().andFactoryNameEqualTo(queryForm.getFactoryWorkshop());
        List<FactoryShift> factoryShifts = factoryShiftMapper.selectByExample(shift);
        for (FactoryShift factoryShift:factoryShifts ){
            FactoryShiftOperateExample operateExample = new FactoryShiftOperateExample();
            operateExample.createCriteria().andShiftIdEqualTo(factoryShift.getId());
            List<FactoryShiftOperate> factoryShiftOperates = factoryShiftOperateMapper.selectByExample(operateExample);
            factoryShift.setFactoryShiftOperates(factoryShiftOperates);
        }
        return factoryShifts;
    }
}
